home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 11.9 KB | 476 lines |
- package symantec.itools.multimedia;
-
-
- import java.awt.Graphics;
- import java.awt.MediaTracker;
- import java.awt.Canvas;
- import java.awt.Image;
- import java.awt.Dimension;
- import java.net.URL;
- import java.util.Vector;
- import java.util.Enumeration;
-
- // 01/29/97 TWB Integrated changes from Windows
-
- /**
- * This is a simple animation component. <br>
- * It creates an animation by displaying a series of images in sequence.
- * The programmer can specify the delay between frames in milliseconds.
- * The animation can loop for a specific
- * number of iterations or can run forever.
- *
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
- public class Animator
- extends Canvas
- implements Runnable
- {
- /**
- * Delay time between images, in milliseconds.
- */
- protected int delay;
-
- /**
- * Number of times to show the animation sequence.
- */
- protected int numLoops;
-
- /**
- * Run animation forever. If false, use numLoops.
- */
- protected boolean forever;
-
- /**
- * Images to be displayed.
- */
- protected Vector images;
-
- /**
- * Image currently being shown.
- */
- protected Image currentImage;
-
- /**
- * Thread which runs the animation.
- */
- protected Thread displayThread;
-
- /**
- * Dimension of largest image in sequence.
- */
- protected int maxWidth, maxHeight;
-
- /**
- * Clear frame between each image.
- */
- protected boolean clearFrame;
-
- /**
- * Preview this component at design time.
- */
- protected boolean previewMode;
-
- /**
- * Constructs a default Animator. The animator defaults to a 500 millisecond delay, and loops forever.
- */
- public Animator()
- {
- delay = 500;
- maxWidth = 0;
- maxHeight = 0;
- numLoops = 1;
- forever = true;
- images = new Vector();
- currentImage = null;
- clearFrame = false;
- previewMode = false;
- }
-
- /**
- * Sets the delay between animation frames.
- * @param i animation delay, in milliseconds
- * @see #getDelay
- */
- public void setDelay(int i)
- {
- delay = i;
- }
-
- /**
- * Returns the current delay between animation frames.
- * @return current animation delay, in milliseconds
- * @see #setDelay
- */
- public int getDelay()
- {
- return delay;
- }
-
- /**
- * Sets the number of loops to perform when displaying
- * the animation set.
- * @param i loop count
- * @see #getNumLoops
- */
- public void setNumLoops(int i)
- {
- numLoops = i;
- }
-
- /**
- * Returns the current animation set loop count.
- * @return loop count
- * @see #setNumLoops
- */
- public int getNumLoops()
- {
- return numLoops;
- }
-
- /**
- * Sets the repeat mode setting.
- * @param b repeat mode, repeats if true
- * @see #getRepeatMode
- */
- public void setRepeatMode(boolean b)
- {
- forever = b;
- }
-
- /**
- * Returns the current repeat mode setting.
- * @return current repeat mode setting, true if repeat forever
- * @see #setRepeatMode
- */
- public boolean getRepeatMode()
- {
- return forever;
- }
-
- void imageLoadWait(Image image)
- {
- MediaTracker tracker = new MediaTracker(this);
-
- tracker.addImage(image, 0);
-
- try
- {
- tracker.waitForAll();
- }
- catch(InterruptedException e)
- {
- }
-
- int size;
-
- if ((size = image.getWidth(this)) > maxWidth)
- maxWidth = size;
-
- if ((size = image.getHeight(this)) > maxHeight)
- maxHeight = size;
- }
-
- /**
- * Adds an image to the animation set.
- * @param url URL of the image to add
- */
- public synchronized void addImage(URL url)
- {
- Image image = getToolkit().getImage(url);
-
- boolean loadWait = images.size() == 0 || previewMode || !symantec.beans.Beans.isDesignTime();
-
- if (loadWait)
- {
- imageLoadWait(image);
- }
-
- images.addElement(new AnimatorImage(url, image, loadWait));
- }
-
- /**
- * Sets the image list. Images in this list are displayed in
- * sequence to form the animation.
- * @param list array of image URLs
- * @see #getImageList
- */
- public synchronized void setImageList(URL[] list)
- {
- boolean wasAnimating = displayThread != null;
-
- if (wasAnimating)
- {
- stopAnimation();
- displayThread = null;
- }
-
- currentImage = null;
- images = new Vector();
-
- for (int i = 0; i < list.length; ++i)
- {
- addImage(list[i]);
- }
-
- if (wasAnimating || previewMode || !symantec.beans.Beans.isDesignTime())
- {
- startAnimation();
- }
- else if (list.length > 0)
- {
- currentImage = ((AnimatorImage)images.elementAt(0)).image;
- }
-
- repaint();
- }
-
- /**
- * Returns the image list.
- * @return URL list of images
- * @see #setImageList
- */
- public synchronized URL[] getImageList()
- {
- URL[] list = new URL[images.size()];
-
- int i = 0;
- Enumeration enum = images.elements();
- while (enum.hasMoreElements())
- {
- list[i] = ((AnimatorImage)enum.nextElement()).url;
- }
-
- return list;
- }
-
- /**
- * Sets whether or not the animation frame area is cleared
- * between each frame.
- * @param b if true, the frame area is cleared between each
- * animation frame; if false, the frame area is not cleared.
- * @see #getClearFrame
- */
- public void setClearFrame(boolean b)
- {
- clearFrame = b;
- }
-
- /**
- * Gets the current clear frame setting.
- * @return boolean - if true, the frame area is cleared
- * between each animation frame; if false, the frame area
- * is not cleared.
- * @see #setClearFrame
- */
- public boolean getClearFrame()
- {
- return clearFrame;
- }
-
- /**
- * Starts the animation.
- * @see #stopAnimation
- */
- public void startAnimation()
- {
- if (displayThread == null)
- {
- displayThread = new Thread(this);
- displayThread.start();
- }
- }
-
- /**
- * Stops the animation.
- * @see #startAnimation
- */
- public void stopAnimation()
- {
- if (displayThread != null)
- {
- displayThread.stop();
- displayThread = null;
- }
- }
-
- /**
- * Sets the preview mode flag. This flag is used by Visual Cafe to
- * determine if this component should be run during design time.
- * @param f new preview mode
- * @see #getPreviewMode
- */
- public void setPreviewMode(boolean f)
- {
- if (symantec.beans.Beans.isDesignTime())
- {
- previewMode = f;
-
- if (previewMode && (images.size() > 0))
- startAnimation();
- else
- stopAnimation();
- }
- }
-
- /**
- * Gets the preview mode flag. This flag is used by Visual Cafe to
- * determine if this component should be run during design time.
- * @param f new preview mode
- * @see #setPreviewMode
- */
- public boolean getPreviewMode()
- {
- return previewMode;
- }
-
- /**
- * Body of Animation Thread. This method is called by the Java Virtual Machine
- * in response to a call to the start method of this object.
- */
-
- public synchronized void run()
- {
- for (int i = 0; i < numLoops || forever; ++i)
- {
- if (images.size() == 0)
- {
- try
- {
- wait(delay);
- }
- catch(InterruptedException e)
- {
- }
-
- }
- else
- {
-
- for (int j = 0; j < images.size(); ++j)
- {
- synchronized(this)
- {
- try
- {
- wait(delay);
- }
- catch(InterruptedException e)
- {
- }
-
- AnimatorImage ai = (AnimatorImage)images.elementAt(j);
-
- if (!ai.loaded)
- {
- imageLoadWait(ai.image);
- images.setElementAt(new AnimatorImage(ai.url, ai.image, true), j);
- }
-
- currentImage = ai.image;
- }
-
- repaint();
- }
- }
- }
- }
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its <0,0>
- * coordinate is this component's top-left corner.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see #update
- */
- public synchronized void paint(Graphics g)
- {
- if (currentImage != null)
- g.drawImage(currentImage, 0, 0, this);
- }
-
- /**
- * Handles redrawing of this component on the screen.
- * This is a standard Java AWT method which gets called by the Java
- * AWT (repaint()) to handle repainting this component on the screen.
- * The graphics context clipping region is set to the bounding rectangle
- * of this component and its <0,0> coordinate is this component's
- * top-left corner.
- * Typically this method paints the background color to clear the
- * component's drawing space, sets graphics context to be the foreground
- * color, and then calls paint() to draw the component.
- *
- * It is overridden here to make clearing the background before painting
- * optional. If the clearFrame flag is true the background will be erased
- * before painting begins.
- *
- * @param g the graphics context
- * @see java.awt.Component#repaint
- * @see #paint
- */
- public void update(Graphics g)
- {
- if (clearFrame)
- super.update(g);
- else
- paint(g);
- }
-
- /**
- * Returns the recommended dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the recommended size of this component.
- *
- * @return If no images have been loaded, a dimension of 10 by 10 is returned.
- * If one or more images have been loaded, the largest height and the
- * largest width of any image is returned.
- *
- * @see #minimumSize
- */
- public Dimension preferredSize()
- {
- if (images == null || images.size() == 0)
- return new Dimension(10, 10);
-
- return new Dimension(maxWidth, maxHeight);
- }
-
- /**
- * Returns the minimum dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the minimum size of this component.
- *
- * @return If no images have been loaded, a dimension of 10 by 10 is returned.
- * If one or more images have been loaded, the largest height and the
- * largest width of any image is returned.
- *
- * @see #preferredSize
- */
- public Dimension minimumSize()
- {
- return preferredSize();
- }
- }
-
-
- class AnimatorImage
- {
- URL url;
- Image image;
- boolean loaded;
-
- public AnimatorImage(URL u, Image i, boolean l)
- {
- url = u;
- image = i;
- loaded = l;
- }
- }
-
-